<?php
namespace Index\Controller;
use Think\Controller;
use Index\Controller\CommonController;
class LoginController extends CommonController {
    public function indexAction(){
        layout(false);
        $str = '99999';
        $this->assign('str',$str);
        $this->display();
    }

    public function captchaAction()
    {
    	$config =    array(
		    'fontSize'    =>    25,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		);
		$Verify =   new \Think\Verify($config);
		$Verify->entry();
    }


    public function addAction()
    {

        $sql ="show tables";
        $res =M()->query($sql);
        var_dump($res);
    	var_dump($_POST);

         // $res =M()->execu($sql);

        // $data =array(
        //     'username'=>'zhibinm',
        //     'email'=>'113664000@qq.com',
        //     );
        // $res = M('user')->add($data);
        // var_dump($res);

        $sql = "select * from user";

        $res =M()->query($sql);
        var_dump($res);

    }

    public function checkdataAction()
    {
    	
    	if(IS_AJAX){
    		$key = current(array_keys($_POST));
    		switch ($key) {
    			case 'username':
    				$this->checkusername();
    				break;
    			case 'email':
    				$this->checkmail();
    				break;
				case 'password':
					$this->checkpassword();
					break;
				case 'captcha':
					$this->checkcaptcha();
					break;
    			default:
    				echo json_encode('所传数据有误');
    				break;
    		}

    	}
    }

    public function checkcaptcha()
    {
    	
    	$code = I('post.captcha','');
		$config =array(
	    	'reset'       =>false,
		);
		$verify = new \Think\Verify($config);
		$res =  $verify->check($code, '');

		if($res){
			$data = array(
			'error'=>1,
			'info'=>'验证码正确'
			);
			echo json_encode($data);
		}else{
			$data = array(
			'error'=>0,
			'info'=>'验证码不正确'
			);
			echo json_encode($data);
		}
    	
    }

    public function checkusername()
    {
    	
    	$username = I('post.username','');
		
		$data = array(
			'error'=>1,
			'info'=>'用户不可用'
			);

		echo json_encode($data);
    	
    }

    public function checkmail()
    {
    	
    	$mail = I('post.mail','');
		
		$data = array(
			'error'=>0,
			'info'=>'邮箱不可用'
			);

		echo json_encode($data);
    	
    }

    public function checkpassword()
    {
    	
    	$password = I('post.password','');
		
		$data = array(
			'error'=>0,
			'info'=>'密码不可用'
			);

		echo json_encode($data);
    	
    }

    public function loginAction()
    {
        $email = I('post.mail','');
        $password = md5(I('post.password',''));

        $sql = "select * from user where email='{$email}' and password='{$password}'";

        $res = M()->query($sql);
        if($res){
            $_SESSION['username']=$res[0]['username'];
            $_SESSION['id']=$res[0]['id'];
            $_SESSION['face']=$res[0]['face'];
            $this->success('登录成功',U('index/index/index'),1);
        }else{
            $this->error('登录失败',U('index/login/index'),6);
        }
    }

    public function loginoutAction()
    {
        session_unset();
        session_destroy();
        $this->success('退出成功',U('index/login/index'),1);
    }

    public function faceAction()
    {
        $this->display();
    }

    public function experienceAction()
    {
        $this->display();
    }

    public function eduAction()
    {
        $this->display();
    }

    public function sendmailAction()
    {
        $url = 'http://127.0.0.1/php/seg/index.php/index/login/getmail';

        $API_USER = 'zhibisdfsdf';
        $API_PWD = 'asfdlkjwlkerjwelkr';

        $param = array(
            'api_user' => $API_USER, # 使用api_user和api_pwd进行验证
            'api_pwd' => $API_PWD,
            'from' => 'support@emailcar.net', # 发信人，用正确邮件地址替代
            'fromname' => 'Emailcar',
            'to' => '113664000@qq.com',# 收件人地址, 用正确邮件地址替代, 多个地址用';'分隔  
            'subject' => '欢迎使用Emailcar',
            'html' => 'Emailcar为您服务！'
        );


        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);

        var_dump($result);
    }

    public function getmailAction()
    {
        // var_dump($_REQUEST);

        $arr = $_REQUEST;
        // $arr = array('code'=>'0','info'=>'你没有权限');
        echo json_encode($arr);
    }


    public function sendqqmailAction()
    {

        $url = "http://127.0.0.1/php/seg/index.php/index/user/checkmail/code/sdfisdfisdff";
        qqmail('6955044@qq.com','test',"这个是一个测试的邮件$url");
    }

    public function qqloginAction()
    {
        qq_login();
    }

    public function callbackAction()
    {
        $Openid = getOpenid();

        $_SESSION['openid']=$Openid;

        $sql = "select * from user where openid='{$Openid}'";

        $res = M()->query($sql);

        if($res){
            $_SESSION['username']=$res[0]['username'];
            $_SESSION['id']=$res[0]['id'];
            $_SESSION['face']=$res[0]['face'];
            $this->success('登录成功',U('index/index/index'));
        }else{
            $this->error('你还没有该网站帐号，请注册或者绑定老帐号',U('index/login/bangding'));
        }


    }

    public function bangdingAction()
    {
        layout(false);
        $user = get_qq_user_info();
        $this->assign('user',$user);
        $this->display('bangding');
    }

}
